<?php

class Shell_Command_Show extends Shell_Command_Abstract_CommandBase implements Shell_Command_Abstract_IShellCommand{
//*************************************************************************************************
		public function __construct() 
		{
			parent::__construct();
			$this->NAME = "show";
		}
		
//*************************************************************************************************
		public function execute($arguments) 
		{
			if (count($arguments) == 0)
			{
				echo "\nNOTE: show cannot be called without any argument! Please, see help.\n\n";
				return;
			}
			
			//$topic = $arguments[count($arguments)-1];
			$topic = $arguments[0];
			
			switch($topic)
			{
				case "ver": 
								$ver = Core_Registry::read(Enum_RegistryItems::VERSION);
								echo "System version: " . $ver . "\n\n";
								break;
								
				case "time": 								
								echo  date("Y-m-d H:i:s") .  "\n\n";
								break;
								
				case "users": 								
								echo  $this->getUsers() .  "\n\n";
								break;
								
				case "user": 								
								$userName = $arguments[1];
								echo  $this->getUser($userName) .  "\n\n";
								break;
								
				case "": 
								echo "show cannot be called without any argument! Please, see help.\n\n";
								break;
				default: echo $topic . " not recognized!\n\n";			
			}
		}
		
//*************************************************************************************************
		private function getUsers()
		{		
			$sqlitePath = Core_Registry::read(Enum_RegistryItems::SQLITE_PATH);				
			$adapter = new System_UMS_SQLiteAdapter($sqlitePath);
			$ums = new System_UMS($adapter);

			$users = $ums->userGetAll();

			// trying to make a table			
			$table = new Zend_Text_Table(array('columnWidths' => array(20, 6, 20)));
			$table->setDecorator('ascii');
			$table->appendRow( array('NAME', 'AGE', 'E-MAIL') );
			
			foreach ($users as $user)
			{
				$table->appendRow( array( $user->getName(), $user->getAge(), $user->getEmail() ) );
			}
			
			return $table->render();		
		}		
		
//*************************************************************************************************
		private function getUser($userName)
		{		
			
			if ( $userName == null || trim($userName) == "" )
			{
				return "\nNo correct username was provided!\n\n";
			}
			
			
			$sqlitePath = Core_Registry::read(Enum_RegistryItems::SQLITE_PATH);				
			$adapter = new System_UMS_SQLiteAdapter($sqlitePath);
			$ums = new System_UMS($adapter);

			$user = $ums->userGet($userName);

			if ($user == null)
			{
				echo "\nUser $user not found!\n\n";
			}
			
			// trying to make a table			
			$table = new Zend_Text_Table(array('columnWidths' => array(20, 6, 20)));
			$table->setDecorator('ascii');
			$table->appendRow( array('NAME', 'AGE', 'E-MAIL') );			

			$table->appendRow( array( $user->getName(), $user->getAge(), $user->getEmail() ) );
			
			return $table->render();
			
		}
		
//*************************************************************************************************		
		public function __toString() 
		{
			return "[Show]";
		}
}